package com.magnificent.atlantis.client.robot.ui.configure;

import com.magnificent.atlantis.client.resources.AtlantisClientResources;
import com.magnificent.atlantis.client.robot.ui.MainFrame;
import com.magnificent.atlantis.client.robot.ui.configure.table.RobotConfigurationTable;
import com.magnificent.atlantis.client.robot.ui.configure.table.RobotConfigurationTableModel;
import com.magnificent.commons.utils.collections.list.MetaArrayList;
import com.magnificent.commons.utils.collections.list.MetaList;
import com.magnificent.commons.utils.collections.list.functions.PredicateFunction;
import com.magnificent.helpers.CommonUIHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * User: Babanin
 * Date: 28.08.2009
 * Time: 0:48:24
 */
public class RobotConfigurationFrame extends JFrame {
    private JButton okButton;
    private JButton exitButton;
    private RobotConfigurationTable userTable;

    public RobotConfigurationFrame() {
        initializeControls();
        initializeView();
    }

    private void initializeView() {
        setTitle("Atlantis Robots");
        setLayout(new BorderLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setIconImage(AtlantisClientResources.ROBOT.getImage());
        getRootPane().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        add(
                CommonUIHelper.packInShiftedPanel(
                        CommonUIHelper.packInTitledPanel(new JScrollPane(userTable), BorderLayout.CENTER, " Robot settings "), 0, 5, 0, 0),
                BorderLayout.CENTER
        );

        add(
                CommonUIHelper.packInPanel(
                        CommonUIHelper.createHorizontalBox(okButton, exitButton),
                        BorderLayout.LINE_END
                ), BorderLayout.SOUTH
        );

        userTable.repaint();

        setPreferredSize(new Dimension(700, 400));
        pack();
        setLocationRelativeTo(null);
    }

    private void initializeControls() {
        userTable = new RobotConfigurationTable(new RobotConfigurationTableModel());

        okButton = new JButton(new AbstractAction("Save") {
            public void actionPerformed(ActionEvent e) {
                MetaList<RobotConfiguration> configurationList =
                        new MetaArrayList<RobotConfiguration>(userTable.getModel().getRobotConfigurations()).
                                where(new PredicateFunction<RobotConfiguration>() {
                                    public boolean predicate(RobotConfiguration configuration) {
                                        return configuration.isActivated();
                                    }
                                });

                RobotConfigurationFrame.this.dispose();

                MainFrame mainFrame = new MainFrame(configurationList);
                mainFrame.setVisible(true);
            }
        });

        exitButton = new JButton(new AbstractAction("Exit") {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }
}
